<?php
	class Player{
	
		private $MAX_COUNT = 6;
	
		private $player = 0;
		private $score = 0;
		private $tempTrumpValue = 0;
		private $shuffle = false;
		private $trumpFound = false;
		private $hand = [];
		
		function __construct(){
		}
		
		function initPlayer($player){
			$this->player = $player;
		}
		
		function resetPlayer(){
			$this->shuffle = false;
			$this->hand = [];
		}
		
		function getAllPlayer(){
			$player = array(
				'player' => $this->player,
				'score' => $this->score,
				'tempTrumpValue' => $this->tempTrumpValue,
				'shuffle' => $this->shuffle,
				'trumpFound' => $this->trumpFound,
				'hand' => $this->hand
			);
			
			return $player;
		}
		
		function setAllPlayer($player){
			$this->player = $player['player'];
			$this->score = $player['score'];
			$this->tempTrumpValue = $player['tempTrumpValue'];
			$this->shuffle = $player['shuffle'];
			$this->trumpFound = $player['trumpFound'];
			$this->hand = $player['hand'];
		}
		
		function getPlayer(){
			return $this->player;
		}
		
		function getScore(){
			return $this->score;
		}
		
		function getShuffle(){
			return $this->shuffle;
		}
		
		function getHand(){
			return $this->hand;
		}
		
		function getHandCount(){
			return count($this->hand);
		}
		
		function getTrumpFound(){
			return $this->trumpFound;
		}
		
		function getTempTrumpValue(){
			return $this->tempTrumpValue;
		}
		
		function setPlayer($player){
			$this->player = $player;
		}
		
		function setScore($score){
			$this->score = $score;
		}
		
		function setShuffle($shuffle){
			$this->shuffle = $shuffle;
		}
		
		function setHand($hand){
			$this->hand = $hand;
		}
		
		function sortHand($index){
			$temp = [];
			array_unshift($temp, $this->hand[$index]);
			unset($this->hand[$index]);
			foreach($this->hand as $h):
				array_push($temp, $h);
			endforeach;
			$this->setHand($temp);
		}
		
		function playHand($deck, $count=0){
			while($count<6 && $deck->getDeckCount()>0){
				array_push($this->hand, $deck->drawCard());
				$count++;
			}
			return $deck;
		}
		
		function shuffleOption(){
			$count = 0;
			
			for($i=0; $i<6; $i++){												// Compares each card in the hand to other cards on hand
				for($j=0; $j<6; $j++){
					if($this->hand[$i]['suit']==$this->hand[$j]['suit'])		// if card's suit matches another card's suit
						$count++;												// increase count
				}																
					if($count<5)												// if count is less than 5 by the end of comparison
						$count = 0;												// reset count
			}
			if($count>=5)
				$this->shuffle = true;
		}
		
		function increaseScore(){
			$this->score = $this->score+1;
		}
		
		function trumpHand($deck){
			for($i=0;$i<6; $i++){												// Checks the player's hands if there's a trump card
				if($this->hand[$i]['suit'] == $deck->getTrump()['suit']){		// If player has trump card
						if($this->trumpFound){									// If there's a previous trump card
						if($this->hand[$i]['value'] < $this->tempTrumpValue)	// compares the previous to current
							$this->tempTrumpValue = $this->hand[$i]['value'];	// sets the temp to the lower value
					}else{														// If there's no previous trump card	
						$this->tempTrumpValue = $this->hand[$i]['value'];		// sets the temp to current value
						$this->trumpFound = true;
					}
					$this->sortHand($i);										// places trump to left most side
				}
			}
		}
	}
?>